package dao.NotaFiscal;


import dao.Item.ItemDaoImpl;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.io.IOException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import pojo.NotaFiscal;
import util.StringToDate;


/**
 *
 * @author Andre Luiz
 */
public class NotaFiscalDaoImpl implements NotaFiscalDao {
    Collection<NotaFiscal> notas;
    
    public NotaFiscalDaoImpl(){
        notas = new ArrayList<>();
    }
    
    /**
     *
     * @param notaFiscal 
     */
    @Override
    public boolean salvar(NotaFiscal notaFiscal) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("smConfeccoesPU");
        EntityManager em = emf.createEntityManager ();
        try{
            em.getTransaction().begin();
            em.persist(notaFiscal);
            em.getTransaction().commit();
            em.close();
            emf.close();
        }catch (RuntimeException e) {
            return false;
        }
        return true;
    }   
    
    @Override
    public ArrayList<NotaFiscal> listar() {
        return (ArrayList) this.notas;
    }

    @Override
    public boolean remover(NotaFiscal notaFiscal) {
        return false;
    }

    @Override
    public NotaFiscal buscar(NotaFiscal notaFiscal) {
        NotaFiscal notaFiscalTemp;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("smConfeccoesPU");
        EntityManager em = emf.createEntityManager ();
        try{
            em.getTransaction().begin();
            notaFiscalTemp = em.find(notaFiscal.getClass(),notaFiscal.getId());
            em.getTransaction().commit();
            em.close();
            emf.close();
        }catch (IllegalArgumentException e) {
            return null;
        }
        return notaFiscalTemp;
    }

    @Override
    public boolean salvarDisco() {
        if(notas == null) return false;
        try {
            FileWriter writer = new FileWriter("registroNotaFiscal");
            PrintWriter saida = new PrintWriter(writer);
            for (NotaFiscal notasIterador : notas) {
                saida.println(notasIterador.getId());
                saida.println(notasIterador.getTotal());
                saida.println(notasIterador.getData().get(Calendar.DAY_OF_MONTH)+"/"+
                              notasIterador.getData().get(Calendar.MONTH)+"/"+
                              notasIterador.getData().get(Calendar.YEAR));
                saida.println(notasIterador.getPedido());
                saida.println("\n");
            }
            writer.close();
            saida.close();
        } catch (IOException e) {
            System.out.println(e.toString());
            return false;
        }
        return true;
    }

    @Override
    public boolean carregarDisco() {
        NotaFiscal nota;
        Calendar dataTemp;
        File arquivo = new File("registroNotaFiscal");
        if(!arquivo.exists()) return false;
        FileReader reader;
        BufferedReader leitor;
        try {
            reader = new FileReader(arquivo);
            leitor = new BufferedReader(reader);
            while(leitor.ready()){
                nota = new NotaFiscal();
                nota.setId(Long.parseLong(leitor.readLine()));
                nota.setTotal(Float.parseFloat(leitor.readLine()));
                dataTemp = StringToDate.convert(leitor.readLine());               
                nota.setData(dataTemp);
                nota.setPedido(Long.parseLong(leitor.readLine()));
                this.salvar(nota);
                leitor.readLine();
                leitor.readLine();
            }
            reader.close();
            leitor.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ItemDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {            
            Logger.getLogger(ItemDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
}

